/**
 * Created by python on 19-4-13.
 */

var vm= new Vue({
    el:"#new",
    delimiters: ['[[', ']]'], // 修改vue模板符号，防止与django冲突
    data:{
        // // 页面中需要使用到的数据，键值对
        host,
        answers:[],
        quest_id:'',
        create_time:'',
        title:'',
        label:'',
        answer_count:'',
        new_time:'',
        answer:'',
        user:'',
        count:0,
		page: 1, // 当前页数
        page_size: 4, // 每页数量
        ordering: '-create_time', // 排序
		num : 0,


    },







        computed: {
        total_page: function(){  // 总页数

            return Math.ceil(this.count/this.page_size);

        },
        next: function(){  // 下一页
            if (this.page >= this.total_page) {
                return 0;
            } else {

                return this.page + 1 ;



            }

        },

        previous: function(){  // 上一页
            if (this.page <= 0 ) {
                return 0;
            } else {
                return this.page - 1;
            }
        },
        page_nums: function(){  // 页码
            // 分页页数显示计算
            // 1.如果总页数<=5
            // 2.如果当前页是前3页
            // 3.如果当前页是后3页,
            // 4.既不是前3页，也不是后3页
            var nums = [];
            if (this.total_page <= 5) {
                for (var i=1; i<=this.total_page; i++){
                    nums.push(i);
                }
            } else if (this.page <= 3) {
                nums = [1, 2, 3, 4, 5];
            } else if (this.total_page - this.page <= 2) {
                for (var i=this.total_page; i>this.total_page-5; i--) {
                    nums.push(i);
                }
            } else {
                for (var i=this.page-2; i<this.page+3; i++){
                    nums.push(i);
                }
            }
            return nums;
        }
    },

    //代码修改地点
    mounted:function () {
        // 一加载就需要做的，直接是代码
        // alert(this.page)
        this.get_data();

    },




    methods:{



        get_data:function () {
                axios.get(host + "/answers/?ordering=-new_answer_time",{
                params: {
                    page: this.page,
                    page_size: this.page_size,
                },
        })
            .then(function (response ) {
                // console.log(response)
                vm.count = response.data.count;


               vm.answers = response.data.results;
                console.log(vm.answers)
            } )


        },


        // 需要用到的函数，键值对 ，键是名称，值是匿名函数
		// 请求数据
        urlNext:function () {
            location.href="./person-loginsign.html?next=/qa-logined.html"
        },
		list:function () {
			vm.num = 0,
            axios.get(host + '/answers/?ordering=-new_answer_time',{
                   params: {
                    page: this.page,
                    page_size: this.page_size,
                },
            })
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

          // 点击页数
        on_page: function (num) {

            if (num != this.page) {
                this.page = num;
                this.get_data();
            }
        },

        // 点击排序
        on_sort: function (ordering) {
            if (ordering != this.ordering) {
                this.page = 1;
                this.ordering = ordering;
                this.get_data();
            }
        },

		hot:function () {
			vm.num = 1,
            axios.get(host + '/answers/?ordering=-answer_count',{
                   params: {
                    page: this.page,
                    page_size: this.page_size,
                },
            })
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },

		wait:function () {
			vm.num = 2,
            axios.get(host + '/answers/?answer_count=0',{
                   params: {
                    page: this.page,
                    page_size: this.page_size,
                },
            })
                .then(function (response) {
                    vm.answers = response.data.results;

                })
                .catch(error => {
                    console.log(error.response.data)
                })
        },



        getDateDiff: function(Time){
	var minute = 1000 * 60;
	var hour = minute * 60;
	var day = hour * 24;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();
	var diffValue = now - Time;
	if(diffValue < 0){return;}
	var monthC =diffValue/month;
	var weekC =diffValue/(7*day);
	var dayC =diffValue/day;
	var hourC =diffValue/hour;
	var minC =diffValue/minute;
	if(monthC>=1){
		result="" + parseInt(monthC) + "月前";
	}
	else if(weekC>=1){
		result="" + parseInt(weekC) + "周前";
	}
	else if(dayC>=1){
		result=""+ parseInt(dayC) +"天前";
	}
	else if(hourC>=1){
		result=""+ parseInt(hourC) +"小时前";
	}
	else if(minC>=1){
		result=""+ parseInt(minC) +"分钟前";
	}else
	result="刚刚";
	return result;
},

  shijianchuo:function (Time) {
      var b = new Date(Time)
      return b.getTime()
  },
	},


})
